//
//  THCPCPublishCountVC.m
//  YJB
//
//  Created by 中电大维 on 2017/1/3.
//  Copyright © 2017年 YJB. All rights reserved.
//

#import "THCPCPublishCountVC.h"
#import "THTaskProtocolVC.h"

@interface THCPCPublishCountVC ()<UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic, strong)NSMutableDictionary *infoDict;
@end

@implementation THCPCPublishCountVC
{
    UITextField *_advertCountField;
    UITextField *_unitPriceField;
    UITextField *_totalPriceField;
    CGFloat     _titleWidth;
    UIButton *iconButton;
    UIButton *agreeButton;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _titleWidth = [NSString widthForString:@"广告数量:" fontSize:15 andHeight:15];
    
    [self setUpNav];
    [self addSubView];
}

- (void)nextClick{//发布任务
    if ([_advertCountField.text intValue] < 10) {
        [MBProgressHUD showError:@"广告数量不能低于10"];
        return;
    }
    if ([_unitPriceField.text intValue] < 10) {
        [MBProgressHUD showError:@"单价不能低于10金币"];
        return;
    }
    if (!agreeButton.selected) {
        [MBProgressHUD showError:@"同意土豪协议才能发布任务"];
        return;
    }
}
- (void)cancelClick{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)agree:(UIButton *)button{
    button.selected = !button.selected;
}
- (void)agreeClick:(UIButton *)button
{
    button.selected = !button.selected;
}
- (void)protocolClick:(UIButton *)button
{
    [self.navigationController pushViewController:[THTaskProtocolVC new] animated:YES];
}

- (void)addicon{
    NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    imagePickerController.sourceType = sourceType;
    [self presentViewController:imagePickerController animated:YES completion:^{
        
    }];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *image =  [info objectForKey:UIImagePickerControllerOriginalImage];
    image = [self compressImage:image toTargetWidth:400];
    [iconButton setImage:image forState:UIControlStateNormal];
}

- (UIImage *)compressImage:(UIImage *)sourceImage toTargetWidth:(CGFloat)targetWidth {
    CGSize imageSize = sourceImage.size;
    
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    
    CGFloat targetHeight = (targetWidth / width) * height;
    
    UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));
    [sourceImage drawInRect:CGRectMake(0, 0, targetWidth, targetHeight)];
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    int total = [_advertCountField.text intValue]*[_unitPriceField.text intValue];
    _totalPriceField.text = [NSString stringWithFormat:@"%d",total];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField.tag == 3) {
        return NO;
    }
    return YES;
}

- (void)addSubView
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    UILabel *advertCountLabel = [[UILabel alloc] init];
    advertCountLabel.text = @"广告数量:";
    advertCountLabel.font = FONT(15);
    advertCountLabel.textColor = [UIColor colorWithHexString:@"ff783c"];
    [view addSubview:advertCountLabel];
    
    _advertCountField = [[UITextField alloc] init];
    _advertCountField.backgroundColor = [UIColor colorWithHexString:@"ff783c" alpha:0.05];
    _advertCountField.placeholder = @"不低于10个";
    _advertCountField.textColor = [UIColor colorWithHexString:@"666666"];
    _advertCountField.font = FONT(15);
    _advertCountField.delegate = self;
    _advertCountField.tag = 1;
    [view addSubview:_advertCountField];
    
    UILabel *unitPriceLabel = [[UILabel alloc] init];
    unitPriceLabel.text = @"单价:";
    unitPriceLabel.textColor = [UIColor colorWithHexString:@"ff783c"];
    unitPriceLabel.font = FONT(15);
    [view addSubview:unitPriceLabel];
    
    _unitPriceField = [[UITextField alloc] init];
    _unitPriceField.backgroundColor = [UIColor colorWithHexString:@"ff783c" alpha:0.05];
    _unitPriceField.placeholder = @"不低于10金币";
    _unitPriceField.textColor = [UIColor colorWithHexString:@"666666"];
    _unitPriceField.font = FONT(15);
    _unitPriceField.delegate = self;
    _unitPriceField.tag = 2;
    [view addSubview:_unitPriceField];
    
    UIView *totalPriceView = [self createView:@"广告总价:"];
    [self.view addSubview:totalPriceView];
    
    _totalPriceField = [[UITextField alloc] init];
    _totalPriceField.delegate = self;
    _totalPriceField.tag = 3;
    [totalPriceView addSubview:_totalPriceField];
    _totalPriceField.placeholder = @"0";
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(52);
    }];
    [advertCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view.mas_centerY);
        make.left.mas_equalTo(12);
        make.width.mas_equalTo(_titleWidth);
        make.height.equalTo(view.mas_height);
    }];
    [_advertCountField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(advertCountLabel.mas_centerY);
        make.left.equalTo(advertCountLabel.mas_right).offset(5);
        make.right.equalTo(view.mas_centerX).offset(-20);
        make.height.equalTo(view.mas_height).multipliedBy(0.65);
    }];
    [unitPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(advertCountLabel.mas_centerY);
        make.left.equalTo(view.mas_centerX);
        make.width.mas_equalTo(_titleWidth*0.6);
        make.height.equalTo(view.mas_height);
    }];
    [_unitPriceField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(advertCountLabel.mas_centerY);
        make.left.equalTo(unitPriceLabel.mas_right).offset(5);
        make.right.equalTo(view.mas_right).offset(-20);
        make.height.mas_equalTo(_advertCountField.mas_height);
    }];
    [totalPriceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_bottom).offset(10);
        make.left.mas_equalTo(0);
        make.height.equalTo(view.mas_height);
        make.width.equalTo(view.mas_width);
    }];
    
    UILabel *label = [totalPriceView viewWithTag:1];
    [_totalPriceField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(totalPriceView.mas_centerY);
        make.left.equalTo(label.mas_right).offset(35);
        make.right.equalTo(totalPriceView.mas_right).offset(-12);
    }];
    
    UIView *iconGroundView = [[UIView alloc] init];
    iconGroundView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:iconGroundView];
    
    [iconGroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(115);
        make.top.equalTo(totalPriceView.mas_bottom).offset(10);
        make.left.right.equalTo(totalPriceView);
    }];
    
    iconButton = [[UIButton alloc] init];
    [iconButton setImage:Image(@"add_image_default") forState:UIControlStateNormal];
    [iconButton addTarget:self action:@selector(addicon) forControlEvents:UIControlEventTouchUpInside];
    [iconGroundView addSubview:iconButton];
    
    [iconButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconGroundView.mas_top).offset(15);
        make.left.equalTo(iconGroundView.mas_left).offset(12);
        make.width.height.mas_equalTo(60);
    }];
    
    
    UILabel *imageAlertLabel = [[UILabel alloc] init];
    imageAlertLabel.text = @"请上传一张广告图标，若没有，可以不上传";
    imageAlertLabel.font = FONT(15);
    [iconGroundView addSubview:imageAlertLabel];
    
    [imageAlertLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconButton.mas_bottom).offset(10);
        make.left.equalTo(iconButton.mas_left);
        make.right.equalTo(iconGroundView.mas_right).offset(-12);
        make.height.mas_equalTo(15);
    }];
    
    NSString *rateStr = @"当前汇率：100金币=1元人民币";
    UILabel *rateLabel = [[UILabel alloc] init];
    rateLabel.font = FONT(14);
    rateLabel.textColor = [UIColor colorWithHexString:@"666666"];
    rateLabel.text = rateStr;
    [self.view addSubview:rateLabel];
    [rateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.top.equalTo(iconGroundView.mas_bottom).offset(20);
        make.right.mas_equalTo(-12);
        make.height.mas_equalTo(15);
    }];
    

    UIEdgeInsets insets = UIEdgeInsetsMake(0, 10, 0, 0);
    NSString *agreeStr = @"我已阅读并同意";
    CGFloat agreeWidth = [NSString widthForString:agreeStr fontSize:14 andHeight:15];
    
    agreeButton = [[UIButton alloc] init];
    [agreeButton setTitleColor:[UIColor colorWithHexString:@"999999"] forState:UIControlStateNormal];
    agreeButton.titleLabel.font = FONT(14);
    [agreeButton addTarget:self action:@selector(agree:) forControlEvents:UIControlEventTouchUpInside];
    [agreeButton setTitleEdgeInsets:insets];
    [agreeButton setImage:Image(@"match_normal") forState:UIControlStateNormal];
    [agreeButton setImage:Image(@"match_selected") forState:UIControlStateSelected];
    [agreeButton setTitle:agreeStr forState:UIControlStateNormal];
    [self.view addSubview:agreeButton];
    agreeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [agreeButton addTarget:self action:@selector(agreeClick:) forControlEvents:UIControlEventTouchUpInside];
    [agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(rateLabel.mas_bottom).offset(15);
        make.left.equalTo(rateLabel.mas_left);
        make.height.mas_equalTo(22);
        make.width.mas_equalTo(agreeWidth+32);
    }];


    NSString *protocolStr = @"《土豪任务管理协议》";
//    CGFloat protocalWidth = [NSString widthForString:protocolStr fontSize:14 andHeight:15];
    UIButton *protocolButton = [[UIButton alloc] init];
    protocolButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [protocolButton setTitleColor:[UIColor colorWithHexString:@"0e41dc"] forState:UIControlStateNormal];
    protocolButton.titleLabel.font = FONT(14);
    [protocolButton setTitle:protocolStr forState:UIControlStateNormal];
    [self.view addSubview:protocolButton];
    [protocolButton addTarget:self action:@selector(protocolClick:) forControlEvents:UIControlEventTouchUpInside];
    [protocolButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(agreeButton.mas_centerY);
        make.left.equalTo(agreeButton.mas_right);
        make.height.mas_equalTo(15);
        make.right.mas_equalTo(-12);
    }];
}
- (void)setUpNav{
    
    self.title = @"发布任务";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"发布" style:UIBarButtonItemStylePlain target:self action:@selector(nextClick)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancelClick)];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor colorWithHexString:@"eeeeee"];
}

- (UIView *)createView:(NSString *)title
{
    UIView *view = [[UIView alloc] init];
    UILabel *label = [UILabel initLabelText:title Font:15 TextColor:[UIColor colorWithHexString:@"ff783c"]];
    label.tag = 1;
    [view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view.mas_centerY);
        make.left.equalTo(view.mas_left).offset(12);
        make.height.mas_equalTo(16);
        make.width.mas_equalTo(_titleWidth);
    }];
    
    view.backgroundColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor whiteColor];
    label.font = FONT(15);
    return view;
}

- (NSMutableDictionary *)infoDict
{
    if (!_infoDict) {
        _infoDict = [NSMutableDictionary dictionary];
    }
    return _infoDict;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
